package heven.holt.wan.android.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.paging.LoadState
import dagger.hilt.android.AndroidEntryPoint
import heven.holt.library.base.BaseFragment
import heven.holt.library.extensions.registerHeader
import heven.holt.library.loading.LoadingStateView
import heven.holt.library.paging.LoadStateFooterMoreAdapter
import heven.holt.wan.android.MainFragment
import heven.holt.wan.android.MainFragmentDirections
import heven.holt.wan.android.adapter.ArticleHomeAdapter
import heven.holt.wan.android.databinding.FragmentHomeBinding
import heven.holt.wan.android.paging.HomeHeadBannerAdapter
import heven.holt.wan.android.viewmodel.HomeViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding>(), LoadingStateView.OnReloadListener {
    private val homeViewModel: HomeViewModel by viewModels()

    private var homeHeadBannerAdapter = HomeHeadBannerAdapter(mutableListOf())

    private val articleHomeAdapter = ArticleHomeAdapter()

    //记录下拉刷新，if true,不刷新父Loading
    private var isPullRefresh = false

    override fun bindViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentHomeBinding = FragmentHomeBinding.inflate(layoutInflater, container, false)

    override fun initView() {
        binding.springView.registerHeader {
            isPullRefresh = true
            onReload()
        }

        val concatAdapter =
            articleHomeAdapter.withLoadStateFooter(LoadStateFooterMoreAdapter {
                articleHomeAdapter.retry()
            })

        articleHomeAdapter.addLoadStateListener {
            if (it.refresh is LoadState.Loading && !isPullRefresh) {
                isPullRefresh = false
                (parentFragment as MainFragment).showLoadingView()
            } else if (it.refresh is LoadState.NotLoading) {
                (parentFragment as MainFragment).showContentView()
                if (!isVisible) return@addLoadStateListener
                binding.springView.onFinishFreshAndLoad()
            } else if (it.refresh is LoadState.Error) {
                (parentFragment as MainFragment).showErrorView()
            }
        }

        articleHomeAdapter.setOnItemClickListener { view, articleModel ->
            val url = articleModel.link ?: return@setOnItemClickListener
            val direction = MainFragmentDirections.actionGlobalWebViewFragment(url)
            view.findNavController().navigate(direction)
        }

        concatAdapter.addAdapter(0, homeHeadBannerAdapter)
        binding.recyclerView.adapter = concatAdapter
    }

    override fun initData() {
        homeViewModel.bannerLiveData.observe(viewLifecycleOwner) {
            homeHeadBannerAdapter.observerBanners(it)
        }

        lifecycleScope.launch {
            homeViewModel.flow.collect { pagingData ->
                articleHomeAdapter.submitData(pagingData)
            }
        }

        if (homeViewModel.bannerLiveData.value == null)
            homeViewModel.requestBanner()
    }

    override fun onReload() {
        homeViewModel.requestBanner()
        articleHomeAdapter.refresh()
    }
}